<?php

namespace app\Home\controller;

use app\common\controller\Home;

class Team extends Home
{
    public function __construct(){
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
    }

    //我的团队
    public function index()
    {
        $list = $this->BusinessModel->where(['parentid' => $this->view->AutoLogin['id']])->select();
        $this->assign('list', $list);
        return $this->view->fetch();
    }

    //海报界面
    public function poster()
    {
        $poster = isset($this->view->AutoLogin['poster']) ? trim($this->view->AutoLogin['poster']) : '';
        $avatar = isset($this->view->AutoLogin['avatar']) ? trim($this->view->AutoLogin['avatar']) : '';
        $avatar = is_file(".".$avatar) ? ".".$avatar : "";

        $id = isset($this->view->AutoLogin['id']) ? trim($this->view->AutoLogin['id']) : 0;
        $mobile = isset($this->view->AutoLogin['mobile']) ? trim($this->view->AutoLogin['mobile']) : '';

        //推广链接
        // http://www.fast.com/home/index/register?busid
        //身份识别 id+mobile 进行md5
        $domain = $this->request->domain();
        $token = md5($id.$mobile);
        $url = "$domain/index.php/home/index/register?token=$token";


        if($this->request->isAjax())
        {
            //生成海报
            $qrcode = new \dh2y\qrcode\QRcode();

            //生成二维码图片
            $level = ['L', 'M', 'Q', 'H'];
            $index = array_rand($level);
            //将url地址写入
            $path = $qrcode->png($url, false, 5, $level[$index])
                ->logo($avatar)  //添加logo图片
                ->getPath();

            //更新到数据库中去
            $data = [
                'id' => $this->view->AutoLogin['id'],
                'poster' => $path
            ];

            //更新
            $result = $this->BusinessModel->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error('更新海报失败');
                exit;
            }else
            {
                //判断海报是否存在,如果存在就删除旧图片
                is_file(".".$poster) && @unlink(".".$poster);

                //返回数据
                $this->success('更新个人海报成功', null, ['poster' => $path]);
                exit;
            }
        }

        //赋值数据
        $this->assign('url', $url);

        return $this->view->fetch();
    }
}
